package com.margo.project.append.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.margo.project.append.utils.LcamGenerateHelper;

@Service
public class LcamDaoFactory {
	
	@Autowired
	private LcamGenerateHelper lcamGenerateHelper;
	
	public void createDAO(String tableName,String packageName){
		StringBuilder sb=new StringBuilder();
		String className=lcamGenerateHelper.initFiledName(tableName.substring(5));
		sb.append("package ").append(packageName).append(".").append(className.toLowerCase()).append(".dao; \r");
		sb.append(" \r");

		sb.append("import java.util.ArrayList; \r");
		sb.append("import java.util.List; \r");
		sb.append("import java.util.Map; \r");

		sb.append("import com.comtop.lcam.common.dao.BeanPropertyMapper; \r");
		sb.append("import com.comtop.lcam.common.dao.IReceiveVO; \r");
		sb.append("import com.comtop.lcam.common.dao.LcamBaseDAO; \r");
		sb.append("import com.comtop.lcam.project.common.util.QueryConditionConvertUtil; \r");
		sb.append("import com.comtop.util.commonservice.QueryCondition; \r");
		
		String voName="Sw"+className+"VO";
		String daoName="Sw"+className+"DAO";
		
		//引入vo
		sb.append("import ").append(packageName).append(".").append(className.toLowerCase()).append(".model.").append(voName).append("; \r");
		sb.append(" \r");

		
		sb.append("public class ").append(daoName).append(" extends LcamBaseDAO{").append(" \r");
		//私有属性
		
		
		String mapper="Sw"+className+"Mapper";
		sb.append("\t").append("private final IReceiveVO<").append(voName).append(">").append(" ").append(mapper).append("=")
		.append("\t\t").append("BeanPropertyMapper.getInstance(").append(voName).append(".class);").append(" \r");
		//列表count方法
		sb.append("\t").append("public int query").append(className).append("Count(Map<String, String> queryMap,")
			.append("List<QueryCondition> condition){").append(" \r");
		sb.append("\t\t").append("StringBuffer sbSQL = new StringBuffer(128);").append(" \r");
		sb.append("\t\t").append("List<Object> lstParams = new ArrayList<Object>();").append(" \r");
		sb.append("\t\t").append("sbSQL.append(\"SELECT COUNT(*) FROM (\");").append(" \r");
		sb.append("\t\t").append("this.initQuerySw").append(className).append("Sql(queryMap, sbSQL, lstParams);").append(" \r");
		sb.append("\t\t").append("QueryConditionConvertUtil objConvert = new QueryConditionConvertUtil(\"").append(voName).append("\", condition);").append(" \r");
		sb.append("\t\t").append("sbSQL.append(objConvert.getSql());").append(" \r");
		sb.append("\t\t").append("sbSQL.append(\") \");").append(" \r");
		sb.append("\t\t").append("List<Object> params = objConvert.getParams();").append(" \r");
		sb.append("\t\t").append("lstParams.addAll(params);").append(" \r");
		sb.append("\t\t").append("int iCount = super.getCount(sbSQL.toString(), lstParams);").append(" \r");
		sb.append("\t\t").append("return iCount;").append(" \r");
		sb.append("\t").append("}").append(" \r");
		
	    //列表list方法
		sb
		.append("\t").append("public List<").append(voName).append("> query").append(className).append("List(Map<String, String> queryMap, List<QueryCondition> condition,").append(" \r")
		.append("\t\t").append(" int pageNo, int pageSize){").append(" \r");
		sb.append("\t\t").append("StringBuffer sbSQL = new StringBuffer(128);").append(" \r");
		sb.append("\t\t").append("List<Object> lstParams = new ArrayList<Object>();").append(" \r");
		sb.append("\t\t").append("this.initQuerySw").append(className).append("Sql(queryMap, sbSQL, lstParams);").append(" \r");
		sb.append("\t\t").append("QueryConditionConvertUtil objConvert = new QueryConditionConvertUtil(\"").append(voName).append("\", condition);").append(" \r");
		sb.append("\t\t").append("List<Object> params = objConvert.getParams();").append(" \r");
		sb.append("\t\t").append("lstParams.addAll(params);").append(" \r");
		sb.append("\t\t").append("List<").append(voName).append("> lstVOs =").append(" \r");
		sb.append("\t\t\t").append("super.getList(").append(mapper).append(", sbSQL.toString(), lstParams, pageNo, pageSize);").append(" \r");
		sb.append("\t\t").append("return lstVOs;").append(" \r");
		sb.append("\t").append("}").append(" \r");
		
		//列表list方法
		sb
		.append("\t").append("private void initQuerySw").append(className).append("Sql(Map<String, String> queryMap, StringBuffer sbSQL, List<Object> lstParams) {").append(" \r")
		.append("\t").append("}").append(" \r");
	  	
		sb.append("}");
		lcamGenerateHelper.fillFile(className.toLowerCase()+"\\dao",daoName,sb);
	}
}
